package top.xchch.tool.mockito.juejin;

import org.junit.Test;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * 根据调用顺序设置不同的stubbing
 */
public class StubbingConsecutiveCallsTest10 {

    @Test
    public void testStubbingConsecutiveCalls() throws Exception {

        MockTest mock = mock(MockTest.class);
        when(mock.someMethod("some arg")).thenThrow(new RuntimeException("")).thenReturn("foo");

        //第一次调用，抛RuntimeException
//        mock.someMethod("some arg");

        //第二次调用返回foo
        System.out.println(mock.someMethod("some arg"));

        //后续继续调用，返回“foo”，以最后一个stub为准
        System.out.println(mock.someMethod("some arg"));

        //下面是一个更简洁的写法
        when(mock.someMethod("some arg")).thenReturn("one", "two", "three");
    }
}

interface MockTest {
    String someMethod(String arg);
}